package com.hxzy.tms.aciton;

import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;

import com.google.gson.Gson;
import com.hxzy.tms.SessionHolder;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.UserService;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends AbstractAction {
    public static final String SESSION_KEY = "SESSION_KEY";

    private static final long serialVersionUID = 2787141219716803023L;

    private String username;
    private String passwd;
    private String remeberMe;

    private UserService userService;

    public String index() throws Exception {
        return INPUT;
    }

    public String post() throws Exception {
        // userService = new DefaultUserServiceImpl();
        User user = userService.login(getUsername(), getPasswd(), ServletActionContext.getRequest().getRemoteAddr());
        ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        ServletActionContext.getResponse().setContentType("application/json");

        if (user != null) {
            ActionContext.getContext().getSession().put(SESSION_KEY, user);
            ServletActionContext.getResponse().getWriter().println("{\"status\":1, \"message\":\"登录成功\"}");
            if ("1".equals(remeberMe)) {
                // 跟新用户，写入 hash 和 cookie 值
                ServiceResult<String> result = userService.generateSessionHash(ipv4());
                if (result.status == 1) {
                    Cookie cookie = new Cookie("hash", result.data);
                    cookie.setMaxAge(5 * (86400)); // 保存 7 天，至少一个礼拜不用登录系统
                    addCookie(cookie);
                }
            }
        } else {
            ServletActionContext.getResponse().getWriter().println("{\"status\":0, \"message\":\"账号或者密码错误\"}");
        }

        return null;
    }

    public String session() throws Exception {
        User user = (User) ActionContext.getContext().getSession().get("user");
        if (user == null) {
            user = userService.getUser(getCookie("hash"), ipv4()).data;
        }
        ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        ServletActionContext.getResponse().setContentType("application/json");

        if (user != null) {
            Gson gson = new Gson();
            ServletActionContext.getResponse().getWriter().println(gson.toJson(user));
        } else {
            ServletActionContext.getResponse().getWriter().println("{}");
        }

        return null;
    }

    public String invalide() throws Exception {
        ((SessionMap<String, Object>) ActionContext.getContext().getSession()).invalidate();
        SessionHolder.clear();
        addCookie(new Cookie("hash", null));
        userService.logout();
        
        ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        ServletActionContext.getResponse().setContentType("application/json");
        ServletActionContext.getResponse().getWriter().println("{\"status\":1, \"message\":\"退出成功\"}");

        return null;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getRemeberMe() {
        return remeberMe;
    }

    public void setRemeberMe(String remeberMe) {
        this.remeberMe = remeberMe;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
